<template>
  <div style="margin: auto; width: 90%; margin-top: 3%">
    <el-form :model="form" label-width="10%" :rules="rules">
      <el-form-item label="账号:" prop="username">
        <el-input
          placeholder="请输入账号"
          v-model="form.username"
          class="el-input"
        ></el-input>
      </el-form-item>
      <el-form-item label="员工姓名:" prop="name">
        <el-input
          placeholder="请输入姓名"
          v-model="form.name"
          class="el-input"
        ></el-input>
      </el-form-item>
      <el-form-item label="手机号:" prop="phone">
        <el-input
          placeholder="请输入手机号"
          v-model="form.phone"
          class="el-input"
        ></el-input>
      </el-form-item>
      <el-form-item label="性别:">
        <el-radio-group v-model="form.sex">
          <el-radio label="1">男</el-radio>
          <el-radio label="0">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="身份证号:" prop="idNumber">
        <el-input
          placeholder="请输入身份证号码"
          v-model="form.idNumber"
          class="el-input"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">立即创建</el-button>
        <el-button @click="cancle">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>
.el-input {
  width: 25%;
}
</style>

<script>
import { addEmployees, getEmployeeById } from "@/api/employee";
export default {
  data() {
    return {
      form: {
        username: "",
        name: "",
        phone: "",
        sex: "1",
        idNumber: "",
      },
      rules: {
        username: [{ required: true, message: "请输入账号", tigger: "change" }],
        name: [{ required: true, message: "请输入姓名", tigger: "change" }],
        idNumber: [
          { required: true, message: "请输入手机号", tigger: "change" },
        ],
        phone: [
          { required: true, message: "请输入身份证号码", tigger: "change" },
        ],
      },
    };
  },
  computed: {
    id() {
      return this.$route.query.id;
    },
  },
  async created() {
    if (this.id) {
      const res = await getEmployeeById(this.id);
      this.form = res.data;
    }
  },
  methods: {
    async submitForm() {
      if (this.id) {
        await changeStatus(this.form);
      } else {
        await addEmployees(this.form);
      }
      this.$router.push("/backend/employee");
      this.$message.success("操作成功");
    },
    cancle() {
      this.$router.push("/backend/employee");
    },
  },
};
</script>
